package com.scau.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.scau.dao.BaseDao;
import com.scau.dao.ISaleOrderDao;
import com.scau.dao.ISaleOrderItemDao;
import com.scau.vo.Admin;
import com.scau.vo.Employee;
import com.scau.vo.SaleOrder;
import com.scau.vo.SaleOrderItem;
import com.scau.vo.ServiceCategory;
import com.scau.vo.Vip;

/**
 * @author Jinglun
 * @create 2017年4月22日
 */
public class SaleOrderDaoImpl extends BaseDao implements ISaleOrderDao {

	private Session session = null;
	private Transaction tx = null;
	private String hql = "";
	// 引入IsaleOrderItemDao,用以查询订单
	private ISaleOrderItemDao saleOrderItemDao;
	
	@Override
	public boolean saveSaleOrder(SaleOrder saleOrder) {
		try {
			session = getSession();
			tx = session.beginTransaction();
			
			HttpServletRequest request = ServletActionContext.getRequest();
			
			String id1 = request.getParameter("saleOrder.barber");
			Integer barber = Integer.parseInt(id1);
			String hql1 = "from Employee em where em.id =  '" + barber + "'";
			Query query1 = session.createQuery(hql1);
			List<Employee> emList = query1.list();
			int emId = emList.get(0).getId();
			Employee em = (Employee) session.get(Employee.class, emId);
			saleOrder.setEmployee(em);
			
			String id2 = request.getParameter("saleOrder.customerNo");
			Integer customerNo = Integer.parseInt(id2);
			String hql2 = "from Vip vip where vip.id = '" + customerNo + "'";
			Query query2 = session.createQuery(hql2);
			List<Vip> vipList = query2.list();
			int vipId = vipList.get(0).getId();
			Vip vip = (Vip) session.get(Vip.class, vipId);
			saleOrder.setVip(vip);
			
			String id3 = request.getParameter("saleOrder.stuff");
			Integer stuff = Integer.parseInt(id3);
			String hql3 = "from Admin ad where ad.id = '" + stuff + "'";
			Query query3 = session.createQuery(hql3);
			List<Admin> adList = query3.list();
			int adId = adList.get(0).getId();
			Admin ad = (Admin) session.get(Admin.class, adId);
			saleOrder.setAdmin(ad);
			
			session.save(saleOrder);
			tx.commit();
			session.close();
			return true;
		} catch (Exception e) {
			tx.commit();
			session.close();
			return false;
		}
	}

	@Override
	public List<SaleOrder> listSaleOrder() {
		session = getSession();
		hql = "from SaleOrder so";
		Query query = session.createQuery(hql);
		List<SaleOrder> soList = query.list();
		
		session.close();
		return soList;
	}
	
	@Override
	public List<SaleOrder> listDebts(){
		session = getSession();
		String status = "未结算";
		hql = "from SaleOrder s where s.status = '" + status +"'";
		Query query = session.createQuery(hql);
		List<SaleOrder> dList = query.list();
		session.close();
		return dList;
	}
	
	@Override
	public List<SaleOrder> getOneSaleOrder(String saleOrderNo){
		session = getSession();
		System.out.println("订单号为"+saleOrderNo);
		hql = "from SaleOrder s where s.saleOrderNo = '" + saleOrderNo + "'";
		
		Query query = session.createQuery(hql);
		List<SaleOrder> soList = query.list();
		
		System.out.println("根据订单号查出："+soList);
		session.close();
		return soList;
	}
	
	@Override
	public boolean updateSO(String saleOrder){
		session = getSession();
		tx = session.beginTransaction();
		
		hql = "from SaleOrder s where s.saleOrderNo = '" + saleOrder + "'";
		Query query = session.createQuery(hql);
		List<SaleOrder> upSO = query.list();
		SaleOrder upSaleOrder = upSO.get(0);
		
		upSaleOrder.setStatus("已交易");
		
		tx.commit();
		session.close();
		return true;		
	}
	
	@Override
	public List<SaleOrder> eachMonthSales(String year){
		session = getSession();
		/*
		 * 从数据库中获得的时间格式为：2017-04-23 09:46:32.0
		 * 
		 * getMonth()方法获取的月份为实际月份减去1
		 * getYear()方法获得的年份是实际年份减去1900
		 * */
		// 用模糊查询查出特定年份的数据
		hql = "from SaleOrder s where s.saleDateTime like '" + year + 
				"%' and s.status = '已交易'";
		Query query = session.createQuery(hql);
		List<SaleOrder> monthSalesList = query.list();
		
		session.close();
		return monthSalesList;
	}
	
	@Override
	public List periodSales(String sql){
		session = getSession();
		
		Query query = session.createSQLQuery(sql);
		List foundList = query.list();
		
		System.out.println("在dao层，查询到的list:" + foundList);
		
		session.close();
		return foundList;
	}
	
	@Override
	public List<ServiceCategory> serCateList(){
		session = getSession();
		hql = "from ServiceCategory sc"; // 查出所有的服务类型
		Query query = session.createQuery(hql);
		//@SuppressWarnings("unchecked")
		List<ServiceCategory> scList = query.list();
		session.close();
		return scList;
	}

	// get,set方法
	public ISaleOrderItemDao getSaleOrderItemDao() {
		return saleOrderItemDao;
	}

	public void setSaleOrderItemDao(ISaleOrderItemDao saleOrderItemDao) {
		this.saleOrderItemDao = saleOrderItemDao;
	}
	
	

}
